<?php
/**
 * MVM_MALL 网上商店系统
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * 
 * @author :     www.mvmmall.com <admin@mvmmall.com> 
 * @version :    v4.X
---------------------------------------------
 */
if(!defined('MVMMALL')){
    exit('Access Denied');
}
//插件的代码必须和文件名保持一致
$convert['mvmmall']['name']    = 'mvmmall';

//编码方式
$convert['mvmmall']['charset']    = 'latin1';
//描述
$convert['mvmmall']['desc']    = 'mvmmall4.0.0商店系统';
//版权信息
$convert['mvmmall']['license']  = 'www.mvmmall.com设计开发';
//接口需要的参数
$convert['mvmmall']['cfg'] = array(
        array('name' => 'con_db', 'value' => '','label'=>'数据库对象'),
        array('name' => 'sprefix', 'value' => '','label'=>'表前缀'),
        array('name' => 'shopdir', 'value' => '','label'=>'商店系统目录'),
        );
    
class mvmmall
{
    var $cfg;
    var $db;//mvmall数据库超作对象
    var $tablepre;//mvmall系统表前缀
    function  mvmmall($cfg = array())
    {
        foreach ($cfg AS $key=>$val)
        {
            $this->cfg[$val['name']] = $val['value'];
        }
        $this->db=$GLOBALS[db];
        $this->tablepre = $GLOBALS[tablepre];
        
    }
    //导入分类数据
    function category(){
        //清空mvmmall分类系统数据
        $this->db->query(" TRUNCATE TABLE {$this->tablepre}category_table");
        //读分类数据，并处理
       $result = $this->cfg[con_db]->query("SELECT * FROM {$this->cfg[sprefix]}category_table");
       while ($list=$this->cfg[con_db]->fetch_array($result)){
           $list[category_name]=iconv('gb2312','UTF-8',$list[category_name]);
           $row=array(
                      'category_id'         => $list[category_id],
                      'category_depth'      => $list[category_depth],
                      'category_name'       => $list[category_name],
                      'category_key'        => $list[category_key],
                      'category_skin'       => 'category.html',
                      'category_rank'       => $list[category_rank],
                      'category_grant'      => $list[category_grant],
					  'category_file1'      => $list[category_file1],
					  'category_file2'      => $list[category_file2],
                      'use_html'            => $list[use_html],
                      'category_head'       => $list[category_head],
                      'category_foot'       => $list[category_foot],
					  'category_admin_memo' => $list[category_admin_memo],
					  'approval_date'       => $list[approval_date],
                      'register_date'       => $list[register_date]
                      );
           //插入mvmall4.1.0系统分类
           $this->db->insert("{$this->tablepre}category_table",$row);
       }
       return true;
    }
    //导入品牌数据
    function brand(){
        return true;
    }
    //会员数据
    function member()
    {
         //清空mvmmall会员数据
        $this->db->query(" TRUNCATE TABLE {$this->tablepre}member_table");
        //会员数据，并处理
       $result = $this->cfg[con_db]->query("SELECT * FROM {$this->cfg[sprefix]}member_table");
       while ($list=$this->cfg[con_db]->fetch_array($result)){
           //判断管理组
           if ($list[member_class]>6){
               $list[adminid]=1;
           }else {
                $list[adminid]=0;
           }
           $row = array(
           'uid'                 => $list[uid],
           'member_class'        => $list[member_class],
           'member_id'           => iconv('gb2312','UTF-8',$list[member_id]),
           'member_pass'         => $list[member_pass],
           'member_name'         => iconv('gb2312','UTF-8',$list[member_name]),
           'member_jumin'        => $list[member_jumin],
           'member_sex'          => $list[member_sex],
           'member_birthday'     => $list[member_birthday],
           'member_tel1'         => $list[member_tel1],
           'member_tel2'         => $list[member_tel2],
           'member_email'        => $list[member_email],
           'member_zip'          => $list[member_zip],
           'member_address'      => iconv('gb2312','UTF-8',$list[member_address]),
           'member_homepage'     => $list[member_homepage],
           'icq'                 =>'',
           'qq'                  =>'',
           'yahoo'               =>'',
           'msn'                 =>'',
           'taobao'              =>'',
           'member_automail'     => $list[member_automail],
           'member_recommend'    => iconv('gb2312','UTF-8',$list[member_recommend]),
           'member_introduction' => iconv('gb2312','UTF-8',$list[member_introduction]),
           'member_point'        => $list[member_point],
           'member_money'        => $list[member_money],
           'member_image'        => $list[member_image],
           'member_login'        => $list[member_login],
           'modify_id'           => $list[modify_id],
           'modify_ip'           => $list[modify_ip],
           'modify_date'         => $list[modify_date],
           'adminid'             => $list[adminid],
           'register_date'       => $list[register_date],
           'myinfo'              => $list[myinfo],
           );
           //插入mvmall4.1.0系统分类
           $this->db->insert("{$this->tablepre}member_table",$row);
       }
       $this->grade();//插入等级
       return true;
    }
    //会员等级
    function grade(){
        //清空mvmmall会员等级
        $this->db->query("TRUNCATE TABLE {$this->tablepre}grade_table");
        //读系统等级并处理
        //$m_class_array = array("客人","临时会员","初级商家","网代商盟","联盟商户","批发商家","供货商","商品管理员","定购管理员","中间管理员","最高管理员","管理员方式");
        $m_class_array  = array("客人","临时会员","普通会员","高级会员","VIP会员","批发商","供货商","商品管理员","定购管理员","中间管理员","最高管理员","管理员方式");
       foreach ($m_class_array as $key=>$val){
           if($key!=0){
           if ($key==3||$key==4||$key==5){
               $show_price=1;
           }else {
               $show_price=0;
           }
           $row = array(
                      group_id         =>$key,
                      group_name       => $val,
                      max_points       => '',
                      min_points       => '',
                      discount         => '',
                      show_price       => $show_price,
                      rank_list        => '',
                      degree           => $key
                      );
             //插入数据
           $this->db->insert("{$this->tablepre}grade_table",$row);           
           }
       }
    }
    
    //导入商品数据
    function goods()
    {
         require_once 'include/upfile.class.php';
         $f = new upfile('gif,jpg,png,bmp','shopimg/gallery/');
         //清空mvmmall商品数据
        $this->db->query("TRUNCATE TABLE {$this->tablepre}goods_table");
         //清空mvmmall相册数据
        $this->db->query("TRUNCATE TABLE {$this->tablepre}gallery");
        
        //清空扩展价格表数据
        $this->db->query("TRUNCATE TABLE {$this->tablepre}enprice");
         //读商品数据，并处理
       $result = $this->cfg[con_db]->query("SELECT * FROM {$this->cfg[sprefix]}goods_table WHERE goods_class != '2' or goods_class != '6'");
       while ($list=$this->cfg[con_db]->fetch_array($result)){
           $thumb_img=str_replace('shopimg/','shopimg/'.date('Ym').'/thumb/', $list[goods_file1]);
           $big_img=str_replace('shopimg/','shopimg/'.date('Ym').'/', $list[goods_file2]);
           $copy_dir1=$this->cfg[shopdir].str_replace('./','/', $list[goods_file1]);//复制图片1
           $copy_dir2=$this->cfg[shopdir].str_replace('./','/', $list[goods_file2]);//复制图片2
           !@copy($copy_dir1,$thumb_img);
           !@copy($copy_dir2,$big_img);
           $row = array(
                   uid                   => $list[uid],
                   use_direct            => $list[use_direct],
                   goods_class           => $list[goods_class],
                   goods_status          => $list[goods_status],
                   goods_category        => $list[goods_category],
                   goods_mcategory1      => $list[goods_mcategory1],
                   goods_mcategory2      => $list[goods_mcategory2],
                   goods_mcategory3      => $list[goods_mcategory3],
                   goods_mcategory4      => $list[goods_mcategory4],
                   goods_rank            => $list[goods_rank],
                   goods_grant           => $list[goods_grant],
                   goods_skin            => 'product.html',
                   goods_code            => $list[goods_code],
                   goods_name            => iconv('gb2312','UTF-8',$list[goods_name]),
                   goods_key             => '',
                   goods_brand           => '',
                   goods_advance         => iconv('gb2312','UTF-8',$list[goods_advance]),
                   goods_add_goods       => $list[goods_add_goods],
                   goods_check_stock     => $list[goods_check_stock],
                   goods_stock           => $list[goods_stock],
                   goods_cost            => $list[goods_cost],
                   goods_market_price    => $list[goods_market_price],
                   goods_sale_price      => $list[goods_sale_price],
                   event_price           => $list[goods_sale_price],
                   price_sale            => '2',
                   goods_point           => $list[goods_point],
                   goods_kg              => $list[goods_kg],
                   goods_free_delivery   => $list[goods_free_delivery],
                   goods_file1           => $thumb_img,
                   goods_file2           => $big_img,
                   goods_movie           => $list[goods_movie],
                   goods_addoption1      => $list[goods_addoption1],
                   goods_addoption2      => $list[goods_addoption2],
                   goods_addoption3      => $list[goods_addoption3],
                   goods_addoption4      => $list[goods_addoption4],
                   use_html              => $list[use_html],
                   goods_main            => iconv('gb2312','UTF-8',$list[goods_main]),
                   goods_head            => iconv('gb2312','UTF-8',$list[goods_head]),
                   goods_foot            => iconv('gb2312','UTF-8',$list[goods_foot]),
                   goods_admin_memo      => iconv('gb2312','UTF-8',$list[goods_admin_memo]),
                   goods_email           => $list[goods_email],
                   goods_supply_id       => $list[goods_supply_id],
                   goods_supply_email    => $list[goods_supply_email],
                   goods_phone           => $list[goods_phone],
                   start_date            => $list[start_date],
                   end_date              => $list[end_date],
                   register_date         => $list[register_date],
                   upv                   => $list[upv],
                   up_date               => $list[up_date],
                   goods_mony            => $list[goods_mony]
                   );
           //插入数据
           $this->db->insert("{$this->tablepre}goods_table",$row);
           //扩展价格
           if ($list[goods_class]=='0'){
             $other_price =array('3'=>$list[hprice],'4'=>$list[vprice],'5'=>$list[pprice]);
            for ($i=3;$i<6;$i++) {
             $this->db->query("INSERT INTO {$this->tablepre}enprice SET goods_id='$list[uid]',group_id='$i',price='$other_price[$i]'");
           }
           }
           
           //上传商品相册
           global $cf_otherw,$cf_otherh;
           $gallery=array($list[goods_file3],$list[goods_file4],$list[goods_file5],$list[goods_file6],$list[goods_file7]);
           foreach ($gallery as $val){
                if ($val!=''){
                $copy_dir=$this->cfg[shopdir].str_replace('./','/', $val);//原图片
                $to_img=str_replace('./shopimg/','shopimg/gallery/',$val);//复制后的图片
                !@copy( $copy_dir,$to_img);
               $f->savename=str_replace('./shopimg/','', $val);//设置缩图文件名
               $thumb = $f->createthumb($to_img,$cf_otherw,$cf_otherh);//处理相册缩图
               $this->db->query("INSERT INTO {$this->tablepre}gallery  SET goods_id='$list[uid]',imgbig='$to_img',thumb='$thumb',img_desc='$desc'");
                }
           }
            //end
       }
      return true; 
    }
    //商品评论
    function gcomment()
    {
        //清空mvmmall商品评论数据
        $this->db->query("TRUNCATE TABLE {$this->tablepre}gcomment_table");
        //读商品评论数据，并处理
        $result = $this->cfg[con_db]->query("SELECT * FROM {$this->cfg[sprefix]}gcomment_table");
        while ($list=$this->cfg[con_db]->fetch_array($result)){
            $row=array(
            guid              => $list[guid],
            comment_id        => $list[comment_id],
            comment_name      => iconv('gb2312','UTF-8',$list[comment_name]),
            comment_star      => $list[comment_star],
            comment_subject   => iconv('gb2312','UTF-8',$list[comment_subject]),
            comment_body      => iconv('gb2312','UTF-8',$list[comment_body]),
            comment_ip        => $list[comment_ip],
            approval_date     => $list[approval_date],
            comment_main      => iconv('gb2312','UTF-8',$list[comment_main]),
            register_date     => $list[register_date],
            );
          //插入数据
           $this->db->insert("{$this->tablepre}gcomment_table",$row);
        }
        return true;
    }
    //页面管理
    function page()
    {
         //清空mvmmall页面管理数据
        $this->db->query("TRUNCATE TABLE {$this->tablepre}page_table");
        //页面管理数据，并处理
        $result = $this->cfg[con_db]->query("SELECT * FROM {$this->cfg[sprefix]}page_table");
        while ($list=$this->cfg[con_db]->fetch_array($result)){
            $row=array(
                      page_grant          => $list[page_grant],
                      page_name           => iconv('gb2312','UTF-8',$list[page_name]),
                      page_key            => $list[page_key],
                      use_html            => $list[use_html],
                      page_subject        => iconv('gb2312','UTF-8',$list[page_subject]),
                      page_body           => iconv('gb2312','UTF-8',$list[page_body]),
                      approval_date       => $list[approval_date],
            );
          //插入数据
           $this->db->insert("{$this->tablepre}page_table",$row);
        }
        return true;
    }
    
    //公告数据
    function notice(){
        $this->board('notice');
        return true;
    }
    
      //留言数据
    function freeboard(){
        $this->board('freeboard');
        return true;
    }
    
    //公告管理
    function board($name)
    {
         //清空管理数据
        $this->db->query("DELETE FROM {$this->tablepre}bmain WHERE ps_name='$name'");
        //读数据
        $result = $this->cfg[con_db]->query("SELECT * FROM {$this->cfg[sprefix]}bmain_$name");
        while ($list=$this->cfg[con_db]->fetch_array($result)){
            $row=array(
                      ps_name         => 'notice',
                      headnum         => $list[headnum],
                      depth           => $list[depth],
                      board_id        => $list[board_id],
                      board_name      => iconv('gb2312','UTF-8',$list[board_name]),
                      board_email     => $list[board_email],
                      board_subject   => iconv('gb2312','UTF-8',$list[board_subject]),
                      use_html        => $list[use_html],
                      use_secret      => $list[use_secret],
                      use_mail        => $list[use_mail],
                      board_category  => $list[board_category],
                      board_fname1    => $list[board_fname1],
                      board_fname2    => $list[board_fname2],
                      register_date   => $list[register_date],
					  approval_date   => $list[approval_date],
                      board_hit       => $list[board_hit],
                      board_vote      => $list[board_vote],
                      board_ip        => $list[board_ip],
                      total_comment   => $list[total_comment],
                      board_body      => iconv('gb2312','UTF-8',$list[board_body]),
            );
          //插入数据
           $this->db->insert("{$this->tablepre}bmain",$row);
        }
        return true;
    }
    
    //友情连接
    function links()
    {
        //清空mvmmall友情连接数据
        $this->db->query("TRUNCATE TABLE {$this->tablepre}forumlinks_table");
        //读友情连接数据，并处理
        $result = $this->cfg[con_db]->query("SELECT * FROM {$this->cfg[sprefix]}forumlinks_table");
        while ($list=$this->cfg[con_db]->fetch_array($result)){
           $copy_dir=$this->cfg[shopdir].str_replace('./','/', $list[logo]);//复制图片1
           $to_dir = $list[logo];
           !@copy($copy_dir,$to_dir);
            $row=array(
            displayorder     => $list[displayorder],
            name             => iconv('gb2312','UTF-8',$list[name]),
            url              => $list[url],
            note             => iconv('gb2312','UTF-8',$list[note]),
            logo             => $to_dir     
            );
          //插入数据
           $this->db->insert("{$this->tablepre}forumlinks_table",$row);
        }
        return true;
    }
    
    //定单数据
    function order_date()
    {
        //清空mvmmall定单数据
        $this->db->query("TRUNCATE TABLE {$this->tablepre}order_info");
        $this->db->query("TRUNCATE TABLE {$this->tablepre}order_goods");
        //读定单数据，并处理
        $result = $this->cfg[con_db]->query("SELECT * FROM {$this->cfg[sprefix]}buyer_table");
        
        $m_payment_array = array("","信用卡结算","在线汇款","即时汇款","手机结算","手机结算","预付款支付","其他方式结算");
        while ($list=$this->cfg[con_db]->fetch_array($result)){
            $row=array(
            ordersn         => $list[buyer_sess],
            order_type      => $list[buyer_class],
            extension_id    => $list[extension_id],
            username        => iconv('gb2312','UTF-8',$list[buyer_name1]),
            buy_address     => iconv('gb2312','UTF-8',$list[buyer_address1]),
            buy_zipcode     => $list[buyer_zip1], 
            buy_tel         => $list[buyer_tel1], 
            buy_mobile      => $list[buyer_tel2],
            buy_email       => $list[buyer_email],
            addtime         => $list[register_date],
            sendtime        => $list[approval_date], 
            delivery_code   => $list[buyer_delivery_confirm],  
            status          => $list[buyer_state],
            consignee       => iconv('gb2312','UTF-8',$list[buyer_name3]),
            address         => iconv('gb2312','UTF-8',$list[buyer_address2]),
            zipcode         => $list[buyer_zip2], 
            tel             => $list[buyer_tel3],
            mobile          => $list[buyer_tel4],
            email           => $list[email],
            sh_id           => $list[sh_id],
            sh_name         => iconv('gb2312','UTF-8',$list[buyer_delivery_info]),
            sh_exes         => $list[buyer_delivery], 
            cas_name        => iconv('gb2312','UTF-8',$list[buyer_wrapping_info]), 
            cas_exes        => $list[buyer_wrapping],
            pay_id          => $list[pay_id],
            pay_name        => $m_payment_array[$list[buyer_payment]],
            goods_amount    => $list[buyer_price],
            order_amount    => ($list[buyer_wrapping]+$list[buyer_delivery]+$list[buyer_price]),
            invoice         => $list[invoice],
            remark          => iconv('gb2312','UTF-8',$list[buyer_main]),                      
            );
          //插入定单数据
           $order_id=$this->db->insert("{$this->tablepre}order_info",$row);
           //插入商品数据
           //1.读数据
           $goods_list=$this->cfg[con_db]->get_one("SELECT * FROM {$this->cfg[sprefix]}purchase_table WHERE purchase_sess = '$list[buyer_sess]'"); 
                $row_goods = array(
                         order_id     => $order_id,
                         goods_id     => $goods_list[purchase_goods_id],
                         goods_name   => iconv('gb2312','UTF-8',$goods_list[purchase_subject]),
                         buy_price    => $goods_list[purchase_price],
                         goods_attr    => $goods_list[purchase_free_delivery],
                         mall_price   => $goods_list[purchase_price],
                         buy_number   => $goods_list[purchase_num],
                         register_date=>$goods_list[register_date],
                            );
             //2.插入数据
            $this->db->insert("{$this->tablepre}order_goods",$row_goods);
        }
        return true;
    }
    
}